//
//  WHStatusPictureView.swift
//  8.31-WH微博
//
//  Created by 吴涵 on 15/9/11.
//  Copyright © 2015年 wuhan. All rights reserved.
//

import UIKit
import SDWebImage

private let WHPictureCellID = "WHPictureCellID"
class WHStatusPictureView: UICollectionView {
    
    var statusViewModel: WHUserStatusModel? {
        didSet{
        sizeToFit()
            reloadData()
        }
    }
    override func sizeThatFits(size: CGSize) -> CGSize {
        return calcViewModel()
    }
    private func calcViewModel() -> CGSize{
        
        let layuot = collectionViewLayout as! UICollectionViewFlowLayout
        
        layuot.itemSize = CGSize(width: WHStatusPictureItemWidth, height: WHStatusPictureItemWidth)
        let count = statusViewModel?.thumbnailURLs?.count ?? 0
        if count == 0 {
        return CGSizeZero
        }
        if count == 1 {
            
        return CGSize(width: WHStatusPictureItemWidth, height: WHStatusPictureItemWidth)
        }
        if count == 4{
         let width = WHStatusPictureItemWidth * 2 + WHStatusPictureItemMargin
            
            return CGSize(width: width, height: width)
        }
        let row = CGFloat((count - 1)/Int(WHStatusPictureMaxCount) + 1)
        
        let h = row * WHStatusPictureItemWidth + (row - 1) * WHStatusPictureItemMargin
        
        let w = WHStatusPictureMaxWidth
        
        return CGSize(width: w, height: h)
    }
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
//        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())

        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        backgroundColor = UIColor.lightGrayColor()
        
        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        
        layout.minimumInteritemSpacing = WHStatusPictureItemMargin
        
        layout.minimumLineSpacing = WHStatusPictureItemMargin
        dataSource = self
        
        registerClass(WHCollectionViewCell.self, forCellWithReuseIdentifier: WHPictureCellID)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

extension WHStatusPictureView: UICollectionViewDataSource {
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return statusViewModel?.thumbnailURLs?.count ?? 0
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(WHPictureCellID, forIndexPath: indexPath) as! WHCollectionViewCell
        cell.imageUrl = statusViewModel?.thumbnailURLs![indexPath.item]
        
        return cell
    }

}
private class  WHCollectionViewCell: UICollectionViewCell {
    
    var imageUrl: NSURL?{
        didSet{
        imageView.sd_setImageWithURL(imageUrl)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(imageView)
        imageView.ff_Fill(self)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


    private lazy var imageView: UIImageView = UIImageView()
}